<?php 
session_start();
if(! isset($_SESSION['mobile'])){
	echo "<script>window.location.href='../view/quick_login.v.php';</script>";
	$_SESSION['userurl'] = $_SERVER['REQUEST_URI'];
	exit;
}
 ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
	<title>天天果园翻版</title>
	<style>
		body{
			margin :0px;
			padding:0px; 
		}

		@media screen and (min-device-width:321px){
			header{
				background:red;
			}		

		}
		ul{
			margin: 0px;
			padding: 0px;
			list-style: none;
		}
	</style>
	<script src="./js/jquery-3.2.1.min.js"></script>
	<link rel="stylesheet" href="./css/shoppingCart.css" />
</head>

<body>
	<header id = "">

	</header>
	<section id = "header_search_box">
		<a href="./index.v.php" id = "header_search_index"></a>
		<span id ="span_myOrchard">购物车</span>
		<a href="./myOrchard.v.php" id = "myOrchard"></a>
	</section>

	<section id="m_cart">
		<div id="m_cartlist" style="display: block;">
			<ul id="goods_list" class="list_unstyled">
<!-- 				<li type="normal">
	                <a href="#">
	                    <img src="./images/1-100x100-2929-UWCW9WDH.jpg" style="display:block;">
	                    <div class="m_cartlist_info">
	                        <h3>玫瑰女皇葡萄</h3>
	                        <h4>规格：2斤</h4>
	                        <h5>￥58.00</h5>
	                    </div>
	                </a>
	                <span class="m_cartlist_delete" data-dismiss="alert">
	                	<img class="delete_img" src="./images/m_cartlist_delete.gif"/>
	                </span>
	                <div class="m_cart_numswrap">
	                    <div class="num_sel_lage pull_right clearfix">
	                        <span class="inC" onclick ="delNum(this)">-</span>
	                        <input id="goods_0" type="text" disabled="" name="qty" autocomplete="on" value="5">
	                        <span class="deC" onclick ="addNum(this)">+</span>
	                    </div>
	                </div>
                </li> -->
<!--                 <li type="normal">
	                <a href="#">
	                    <img class="lazy pull_left" src="./images/1-100x100-103-8UAUAW81.jpg" style="display:block;">
	                    <div class="m_cartlist_info">
	                        <h3>沙地红蜜薯</h3>
	                        <h4>规格：2斤</h4>
	                        <h5>￥15.80</h5>
	                    </div>
	                </a>
	                <span class="m_cartlist_delete" data-dismiss="alert">
	                	<img class="delete_img" src="./images/m_cartlist_delete.gif"/>
	                </span>
	                <div class="m_cart_numswrap">
	                    <div class="num_sel_lage pull_right clearfix">
	                        <span class="inC" onclick ="delNum(this)">-</span>
	                        <input id="goods_1" type="text" disabled="" name="qty" autocomplete="on" value="1">
	                        <span class="deC" onclick ="addNum(this)">+</span>

	                    </div>
	                </div>
                </li> -->
			</ul>
		</div>
		<div id="m_carttips" style="display: block;">
			<ul class="list_unstyled">
				<li>
					<a href="#">
						<i>换</i>
						
						<span name = "tips">满59元+9.9元换购卡士鲜酪乳发酵乳（原味）三联杯</span>
						<span class="arr_right"></span>
					</a>
				</li>
				<li>
					<a href="#">
						<i>换</i>
						
						<span name = "tips">满79元+14.9元换购北极甜虾250g</span>
						<span class="arr_right"></span>
					</a>
				</li>
				<li>
					<a href="#">
						<i>换</i>
						<span name = "tips">满119元+19.9元换购深井冰椰青2个</span>
						<span class="arr_right"></span>
					</a>
				</li>
				<li>
					<a href="#">
						<i>换</i>
						
						<span name = "tips">满158元+19.9元换购新疆西州蜜瓜1个</span>
						<span class="arr_right"></span>
					</a>
				</li>
			</ul>
		</div>
		<div id="m_noGoods" class="text-center" style="display: none;">
			<span ><img class="iconShopCart" src="./images/2017-09-29_100007.gif" alt="" /></span>
			<h4>您的购物车现在是空的噢~</h4>
			<h5>现在就去选购吧</h5>
			<a href="./index.v.php" class="btn btn-warning navbar-btn">去逛逛</a>
		</div>		
	</section>
	<footer id="shoppingCart_footer" style="display: block;">
	    <div id="footer_left">
	    	合计：￥<span name="total">305.80</span>
	        <p>商品总价￥<span name="total">305.80</span>， 优惠￥0.00</p>
	    </div>
	    <button type="button" id="footer_right" onclick="window.location.href='./order.v.php' ">
	    	去结算
		</button>
	</footer>
	<script>
		function initShopCart(){
			$.get('../api/shopCartApi.php',{'act':'initShopCart'},function(data){
				// alert(data.data)
				if(data.code==100){
					alert(data.msg)
					window.location.href="./quick_login.v.php"
				}
				if(data.code==105){
					var jsonRows=data.data
					// alert(data.msg)
					// alert(jsonRows)
					makeLi(jsonRows)
				}
				if(data.code==104){
					$("#m_cartlist").css("display","none")
					$("#m_carttips").css("display","none")
					$("#shoppingCart_footer").css("display","none")
					$("#m_noGoods").css("display","block")
				}
			},'json')				
		}
		$(initShopCart)
		function makeLi(arr){
			var tab=''
			var money=0
			for(var i in arr){
				var row=arr[i]
				money+=parseFloat(row.price) * parseInt(row.num)
				var goodname=""
				if(row.goodname.length>12){
					goodname=row.goodname.substr(0,12)+"..."
				}
				tab+='<li type="normal"><a id="'+row.goodid+'" href="details.v.php?id='+row.goodid+'">'+row.img+'<div class="m_cartlist_info"><h3>'+goodname+'</h3><h4>规格：'+row.nums+''+row.pcs+'</h4><h5>￥'+row.price+'</h5></div></a><span class="m_cartlist_delete" data-dismiss="alert"><img onclick="delGoods(this)" class="delete_img" src="./images/m_cartlist_delete.gif"/></span><div class="m_cart_numswrap"><div class="num_sel_lage pull_right clearfix"><span class="inC" onclick ="delNum(this)">-</span><input id="goods_0" type="text" disabled="" name="qty" autocomplete="on" value="'+row.num+'"><span class="deC" onclick ="addNum(this)">+</span><input type="hidden" id="'+row.goodid+'" name="'+row.goodid+'" value='+row.goodid+'></div></div></li>'
			}
			for(j = 0; j < $("span[name='total']").length; j++){
				$("span[name='total']").eq(j).html(parseFloat(money).toFixed(2))
			}
			$("#goods_list").html(tab)
			$("#m_cartlist ul li a img").addClass("lazy pull_left")
			$("#m_cartlist ul li a img").css("display","block")
			$("#m_cartlist").css("display","block")
			$("#m_carttips").css("display","block")
			$("#shoppingCart_footer").css("display","block")
			$("#m_noGoods").css("display","none")
		}
	
		function delNum(ts){
			//可以写成jQuery事件
			var num = parseInt($(ts).parent().children().eq(1).val())
			if(num > 1){
				num = parseInt(num)-1
				$(ts).parent().children().eq(1).val(num)
				 $(total())
			}
			var goodid=$(ts).parent().children().eq(3).val()
			$.get('../api/shopCartApi.php',{'act':'changeNum','goodid':goodid,'num':num},function(data){
			},"json")	
		}
		function addNum(ts){
			var num = parseInt($(ts).parent().children().eq(1).val())
			num = parseInt(num)+1
			$(ts).parent().children().eq(1).val(num)
			$(total())
			var goodid=$(ts).parent().children().eq(3).val()
			$.get('../api/shopCartApi.php',{'act':'changeNum','goodid':goodid,'num':num},function(data){
			},"json")			
		}
		function total(){
			var money = 0 
			for(i = 0; i < $("#goods_list h5").length; i++){
				money += parseFloat($("#goods_list h5").eq(i).text().substr(1)) * parseInt($("input[name='qty']").eq(i).val())
			}
			for(j = 0; j < $("span[name='total']").length; j++){
				$("span[name='total']").eq(j).html(parseFloat(money).toFixed(2))
			}
		}		
		function hideStr(){
			var obj = document.getElementsByName("tips")
			for(i=0;i<obj.length;i++){
				if(obj[i].innerText.length > 20){
					obj[i].innerText = obj[i].innerText.substr(0,20)+"..."
				}
			}
		
		}
		$(hideStr)
		function delGoods(ts){
			// Ext.MessageBox.confirm("确认", "请点击下面的按钮作出选择");
			var a=confirm('亲，您真的要删除吗~')
			if(a==true){
				var goodid=$(ts).parent().siblings().attr('id')
				$(ts).parent().parent().remove()
				$(total())
				if($("#goods_list li").length<1){
					$("#m_cartlist").css("display","none")
					$("#m_carttips").css("display","none")
					$("#shoppingCart_footer").css("display","none")
					$("#m_noGoods").css("display","block")				
				}
				$.get('../api/shopCartApi.php',{'act':'delGoods','goodid':goodid},function(data){
				},"json")					
			} else{

			}


		}
		
	</script>
</body>
</html>